package org.one.stone.soup.grfxML.plugin.swing;

import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JDialog;
import javax.swing.JTextArea;

import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.DataText;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.stringhelper.StringArrayHelper;
import org.one.stone.soup.swing.JTextConsole;

public class JTextAreaPlugin extends SimplePlugin implements Runnable,KeyListener{
	private static final int MESSAGE_ARG = 0;
	private static final int TARGET_ARG = 1;
	private static final int ENTER_ARG = 2;

	private DataString message = new DataString();
	private DataText target = new DataText();
	private DataState enter = new DataState();

	private JTextConsole console;
/**
 * FileToText constructor comment.
 */
public JTextAreaPlugin(GrfxMLEngine engine) {
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize() {}
/**
 * process method comment.
 */
public void process()
{
 try{
	if(enter.getValue()==true)
	{
		new Thread(this).start();
		enter.setValue(false,this);
	}
 }catch(LockException le)
 {}
}

public void run()
{
		JDialog dialog = new JDialog((Frame)null,message.getValue(),true);
		console = new JTextConsole();
		dialog.getContentPane().add(console);

		StringBuffer text = new StringBuffer();
		for(int loop=0;loop<target.size();loop++)
		{
			text.append( target.get(loop).getValue() );
			text.append( "\n" );
		}

		console.setText(text.toString());
		console.addKeyListener(this);

		dialog.pack();
		dialog.setVisible(true);
}

/**
 * register method comment.
 */
public void register(org.one.stone.soup.mjdb.data.field.DataLinker store)
{
	message = grfxMLCaster.cast(message,getArg(message,MESSAGE_ARG,store));
	target = grfxMLCaster.cast(target,getArg(target,TARGET_ARG,store));
	enter = grfxMLCaster.cast(enter,getArg(enter,ENTER_ARG,store));
}
public void replace(Field oldObj, Field newObj)
{
	message = grfxMLCaster.replace(message,oldObj,newObj);
	target = grfxMLCaster.replace(target,oldObj,newObj);
	enter = grfxMLCaster.replace(enter,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	 */
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	 */
	public void keyReleased(KeyEvent e) {
		try{
			target.setEmpty();

			String[] lines = StringArrayHelper.parseFields(console.getText(),'\n');

			for(int loop=0;loop<lines.length;loop++)
			{
				target.add(new DataString(lines[loop]),this);
			}
		}
		catch(Exception ex)
		{}
	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	 */
	public void keyTyped(KeyEvent e) {
	}

}
